package com.gzhryc.servlet.resources;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gzhryc.common.json.JsonTools;
import org.apache.http.HttpHeaders;


import com.gzhryc.servlet.Resource;

/**
 * JSON资源类
 * @author 张浩
 * @version 1.3.2
 */
public class JsonResource extends Resource{
	
	private Object obj;
	
	public JsonResource(Object obj){
		super("application/json");
		this.obj = obj;
	}

	@Override
	public void export(HttpServletRequest request,HttpServletResponse response) throws Exception{
		if(obj != null) {
			String json = JsonTools.toJson(obj);
			response.addHeader(HttpHeaders.CONTENT_TYPE,this.getMediaType());
			response.setCharacterEncoding(this.getCharset().name());
			OutputStream out = response.getOutputStream();
			out.write(json.getBytes(charset));
			out.flush();
		}
	}
}
